github action部署到vps
2022年03月17日187 次阅读1 人喜欢
技术
在 ECS 上创建用户,并设置密码
adduser github-actionasswd passwd github-action
免密登陆
- 生成密钥
bash
ssh-keygen -t rsa -C 'github-action' -f ~/.ssh/github_action
- 复制密钥到远程服务器
bash
# ssh-copy-id
ssh-copy-id -i ~/.ssh/github_action.pub github-action@nnnnzs.cn
- 设置默认登录
因为同一台电脑可能既有 root 也有其他用户登录 ecs 所以可以配置指定 Host,指定 User,使用不同的密钥
yaml
Host nnnnzs.cn
HostName github-action
User github-action
Port 22
PreferredAuthentications publickey
IdentityFile /Users/nnnnzs/.ssh/github_action
UseKeychain yes
AddKeysToAgent yes
赋予部署文件夹权限
bash
sudo chmod -R 777 /dist
设置github-action的 secrets
https://github.com/NNNNzs/upload/settings/secrets/actions
编写自动部署脚本
mkdir -p .github/workflows
touch .github/workflows/build.yml
yml
name: vue3
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: install
run: npm install
- name: build
run: npm run build
- name: deploy
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{secrets.KEY}}
port: ${{ secrets.PORT }}
source: "./dist/"
target: ${{secrets.PATH}}
bash
curl -H "Content-Type:application/json" -X POST -d '{"title":"自动部署","content":"upload部署完成","url":"https://api.nnnnzs.cn/upload"}' https://api.nnnnzs.cn/Api/msg
curl -H "Content-Type:application/json" -X POST -d '{"uid":"123"}' http://127.0.0.1:3000/rest/test